package learn.java.basic.net;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

@Slf4j
public class LocalClient {

    public void sendMsg(int i){
        Socket socket = new Socket();
        try {
            socket.connect(new InetSocketAddress( 8080));
            if(socket.isConnected()){
                OutputStream outputStream = socket.getOutputStream();
                byte []bytes = ("socket:"+i+Thread.currentThread().getName()).getBytes(StandardCharsets.UTF_8);
                outputStream.write(bytes);
                outputStream.flush();
                socket.close();
            }
        } catch (IOException e) {
            log.error("{}",e);
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new LocalClient().sendMsg(i);
        }
    }
}
